<?php
require 'config.php';
$user_id = $_REQUEST['user_id'];
$orderId = $_REQUEST['orderId'];
$shuoming = $_REQUEST['shuoming'];
$yy = $_REQUEST['yy'];
// $src1=$_REQUEST['src1'];
// $src2=$_REQUEST['src2'];
// $src3=$_REQUEST['src3'];
$src1 = '';
$src2 = '';
$src3 = '';
if (isset($_REQUEST['src1'])) {
    $src1 = $_REQUEST['src1'];
}

if (isset($_REQUEST['src2'])) {
    $src2 = $_REQUEST['src2'];
}

if (isset($_REQUEST['src3'])) {
    $src3 = $_REQUEST['src3'];
}

$createTime = date('Y-m-d H:i:s');
$sql = "select * from `" . $oto . "_orders` where `orderId`='" . $orderId . "' LIMIT 1";
$result = $db->query($sql);
while ($row = $result->fetch_assoc()) {
    $order_info[] = $row; //将取得的所有数据赋值给person_info数组
}
$sql = "select * from `" . $oto . "_users` where `userId`='" . $user_id . "' LIMIT 1";
$result = $db->query($sql);
while ($row = $result->fetch_assoc()) {
    $user_info[] = $row; //将取得的所有数据赋值给person_info数组
}
if (!isset($order_info)) {
    $data[0]['pan'] = 0;
} else {
    $time = time();
    $money = $order_info[0]['needPay'];
    $shopId = $order_info[0]['shopId'];
    $images_src = '';
    if ($src1 != "") {
        $images_src = $src1 . '|' . $src2 . '|' . $src3;
    }
    if ($order_info[0]['orderStatus'] == 0) //直接退款
    {
        // $sql="update ".$oto."_orders set orderStatus='-1',isRefund='1' where `orderId`='".$orderId."'";
        // $result=$db->query($sql);
        // $sql="INSERT INTO ".$oto."_refund (`orderId`,`time`,`reason`,`explain`,`money`,`userid`,`shopid`,`biz_status`,`actual_money`,`way`,`images`) VALUES ('$orderId','$time','$yy','$shuoming','$money','$user_id','$shopId','0','0.00','0','$images_src')";
        //             $result=$db->query($sql);
        //             $data[0]['pan']=1;

        $sql = "update " . $oto . "_orders set orderStatus='-6',isRefund='2' where `orderId`='" . $orderId . "'";
        $result = $db->query($sql);
        $sql = "INSERT INTO " . $oto . "_refund (`orderId`,`time`,`reason`,`explain`,`money`,`userid`,`shopid`,`biz_status`,`actual_money`,`way`,`biz_time`,`images`) VALUES ('$orderId','$time','$yy','$shuoming','$money','$user_id','$shopId','1','$money','0','$time','$images_src')";
        $result = $db->query($sql);
        $user_money = $user_info[0]['userMoney'] + $money;
        $sql = "update " . $oto . "_users set userMoney='{$user_money}' where `userId`='" . $user_id . "'";
        $result = $db->query($sql);

        $orderNo = $order_info[0]['orderNo'];
        $sql = "INSERT INTO " . $oto . "_money_record (`type`,`money`,`time`,`orderNo`,`IncDec`,`userid`,`balance`,`remark`,`payWay`) VALUES ('2','$money','$time','$orderNo','1','$user_id','$user_money','订单退款','0')";
        $result = $db->query($sql);
        $data[0]['pan'] = 1;

        $content = '用户取消订单：' . $shuoming;
        $sql = "INSERT INTO " . $oto . "_log_orders (`orderId`,`logContent`,`logUserId`,`logType`,`logTime`) VALUES ('$orderId','$content','$user_id','0','$createTime')";
        $result2 = $db->query($sql);
        if ($order_info[0]['deliverType'] == 1) {
            changePtOrder($orderId, 2);
        }
    } else if ($order_info[0]['orderStatus'] == 1) //等待商户同意
    {
        $sql = "update " . $oto . "_orders set orderStatus='-6',isRefund='1' where `orderId`='" . $orderId . "'";
        $result = $db->query($sql);
        $sql = "INSERT INTO " . $oto . "_refund (`orderId`,`time`,`reason`,`explain`,`money`,`userid`,`shopid`,`biz_status`,`actual_money`,`way`,`images`) VALUES ('$orderId','$time','$yy','$shuoming','$money','$user_id','$shopId','0','0.00','0','$images_src')";
        $result = $db->query($sql);
        $data[0]['pan'] = 1;
        $content = '用户取消订单：' . $shuoming;
        $sql = "INSERT INTO " . $oto . "_log_orders (`orderId`,`logContent`,`logUserId`,`logType`,`logTime`) VALUES ('$orderId','$content','$user_id','0','$createTime')";
        $result2 = $db->query($sql);
    } else if ($order_info[0]['orderStatus'] == 2) //等待商户同意
    {
        $sql = "update " . $oto . "_orders set orderStatus='-7',isRefund='1' where `orderId`='" . $orderId . "'";
        $result = $db->query($sql);
        $sql = "INSERT INTO " . $oto . "_refund (`orderId`,`time`,`reason`,`explain`,`money`,`userid`,`shopid`,`biz_status`,`actual_money`,`way`,`images`) VALUES ('$orderId','$time','$yy','$shuoming','$money','$user_id','$shopId','0','0.00','0','$images_src')";
        $result = $db->query($sql);
        $data[0]['pan'] = 1;
        $content = '用户取消订单：' . $shuoming;
        $sql = "INSERT INTO " . $oto . "_log_orders (`orderId`,`logContent`,`logUserId`,`logType`,`logTime`) VALUES ('$orderId','$content','$user_id','0','$createTime')";
        $result2 = $db->query($sql);
    } else if ($order_info[0]['orderStatus'] == 3) //等待商户同意
    {
        $sql = "update " . $oto . "_orders set orderStatus='-3',isRefund='1' where `orderId`='" . $orderId . "'";
        $result = $db->query($sql);
        $sql = "INSERT INTO " . $oto . "_refund (`orderId`,`time`,`reason`,`explain`,`money`,`userid`,`shopid`,`biz_status`,`actual_money`,`way`,`images`) VALUES ('$orderId','$time','$yy','$shuoming','$money','$user_id','$shopId','0','0.00','0','$images_src')";
        $result = $db->query($sql);
        $data[0]['pan'] = 1;
        $content = '用户拒收：' . $shuoming;
        $sql = "INSERT INTO " . $oto . "_log_orders (`orderId`,`logContent`,`logUserId`,`logType`,`logTime`) VALUES ('$orderId','$content','$user_id','0','$createTime')";
        $result2 = $db->query($sql);
    } else if ($order_info[0]['orderStatus'] == 4) //等待商户同意
    {
        $sql = "update " . $oto . "_orders set orderStatus='-8',isRefund='1' where `orderId`='" . $orderId . "'";
        $result = $db->query($sql);
        $sql = "INSERT INTO " . $oto . "_refund (`orderId`,`time`,`reason`,`explain`,`money`,`userid`,`shopid`,`biz_status`,`actual_money`,`way`,`images`) VALUES ('$orderId','$time','$yy','$shuoming','$money','$user_id','$shopId','0','0.00','0','$images_src')";
        $result = $db->query($sql);
        $data[0]['pan'] = 1;
        $content = '用户申请退款：' . $shuoming;
        $sql = "INSERT INTO " . $oto . "_log_orders (`orderId`,`logContent`,`logUserId`,`logType`,`logTime`) VALUES ('$orderId','$content','$user_id','0','$createTime')";
        $result2 = $db->query($sql);
    } else if ($order_info[0]['orderStatus'] == 5 && $order_info[0]['deliverType'] == 1) {// 平台订单退货标识乘二变负数
        $orderStatus = -10;
    } else if ($order_info[0]['orderStatus'] == 6 && $order_info[0]['deliverType'] == 1) {
        $orderStatus = -12;
    } else if ($order_info[0]['orderStatus'] == 7 && $order_info[0]['deliverType'] == 1) {
        $orderStatus = -14;
    } else if ($order_info[0]['orderStatus'] == 8 && $order_info[0]['deliverType'] == 1) {
        $orderStatus = -16;
    }

    if (($order_info[0]['orderStatus'] == 5 || $order_info[0]['orderStatus'] == 6 || $order_info[0]['orderStatus'] == 7 || $order_info[0]['orderStatus'] == 8) && $order_info[0]['deliverType'] == 1) {
        $sql = "update " . $oto . "_orders set orderStatus='" . $orderStatus . "',isRefund='1' where `orderId`='" . $orderId . "'";
        $result = $db->query($sql);
        $data[0]['pan'] = 1;
    }

}
echo json_encode($data);
